import cv2 as cv
def face_check():
    # 灰度图片
    gray = cv.cvtColor(ret, cv.COLOR_BGR2GRAY)
    #加载特征数据，来自opencv中一个默认的扫描机制
    face_detector = cv.CascadeClassifier("D:/OpenCV/opencv/opencv/data/haarcascades/haarcascade_frontalface_alt2.xml")
    #检测
    faces = face_detector.detectMultiScale(gray)
    #针对人脸划分区域
    for x,y,w,h in faces:
        cv.rectangle(ret, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)
    #检测完后进行显示
    cv.imshow("result", ret)

#加载图片
ret = cv.imread("jaychou3.jpg")
#检测人脸
face_check()
cv.waitKey(0)
cv.destroyAllWindows()